package com.dji.sample.component.listener;

import com.dji.sample.common.constant.CommonConstant;
import com.dji.sample.component.listener.handle.EndLiveHandle;
import com.dji.sample.component.listener.handle.FireInfoHandle;
import com.dji.sample.component.listener.handle.StartLiveHandle;
import com.dji.sample.component.redis.RedisOpsUtils;
import com.dji.sample.component.websocket.service.ISendMessageService;
import com.dji.sample.component.websocket.service.IWebSocketManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;

/**
 * @author yjt
 * @date 2023-02-09
 * @description:
 */
@Component
@Slf4j
public class RedisListener implements MessageListener {

    @Autowired
    private ISendMessageService sendMessageService;

    @Autowired
    private IWebSocketManageService webSocketManageService;

    @Autowired
    private RedisOpsUtils redisOps;

    @Override
    public void onMessage(Message message, byte[] pattern) {

        byte[] messageBody = message.getBody();
        String msg = new String(messageBody, StandardCharsets.UTF_8);
        // 获取监听的频道
        byte[] channelByte = message.getChannel();
        String topic = new String(channelByte, StandardCharsets.UTF_8);
        log.info("==接收到消息=topic=={}=msg={}", topic, msg);
        switch (topic) {
            case CommonConstant.START_LIVE:
                new StartLiveHandle().dealMsg(msg, topic, sendMessageService, webSocketManageService,redisOps);
                break;
            case CommonConstant.END_LIVE:
                new EndLiveHandle().dealMsg(msg, topic, sendMessageService, webSocketManageService,redisOps);
                break;
            case CommonConstant.FIRE_INFO:
                new FireInfoHandle().dealMsg(msg, topic, sendMessageService, webSocketManageService,redisOps);
                break;
            default:
                log.info(":未知的topic:{}", topic);
        }
    }

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory factory,
                                                   RedisListener listener
    ) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(factory);
        container.addMessageListener(listener, Arrays.asList(new ChannelTopic(CommonConstant.START_LIVE), new ChannelTopic(CommonConstant.END_LIVE),new ChannelTopic(CommonConstant.FIRE_INFO)));
        return container;
    }
}
